//
//  PickerViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/6/27.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "PickerViewController.h"

@interface PickerViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    
}
@property (nonatomic,strong) NSString *shopName;
@property (nonatomic,strong) NSString *shopId;
@end

@implementation PickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self uiSetting];
}

#pragma mark - 点击事件 
- (IBAction)backbuttonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)cancelButtonClick:(UIButton *)sender {
    if (_type.integerValue == 3) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"wifi"];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

//确定
- (IBAction)sureButtonClick:(UIButton *)sender {
    if (_type.integerValue == 1) {
        NSDictionary *dic = @{
                              @"name" : _shopName,
                              @"id" : _shopId
                              };
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shopNameFromPicker" object:dic];
    }else if (_type.integerValue == 2){
        _shopId = [@"自谈" isEqualToString:[BGFunctionHelper isNULLOfString:_shopName] ? @"自谈" : _shopName] ? @"0" : @"1";
//        NSDictionary *dict = @{
//                               @"indexPath" : _indexpath,
//                               @"name" : [BGFunctionHelper isNULLOfString:_shopName] ? @"自谈" : _shopName,
//                               @"id" : _shopId
//                               };
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"place" object:dict];
        
        [_delegate getShopFromInfoWithTypeValue:[BGFunctionHelper isNULLOfString:_shopName] ? @"自谈" : _shopName typeId:_shopId];
        
    }else if (_type.integerValue == 3){
        NSDictionary *dic = @{
                              @"name" : _shopName,
                              @"id" : _shopId
                              };
        [[NSNotificationCenter defaultCenter] postNotificationName:@"wifiName" object:dic];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark - pickerView设置

- (void)uiSetting{
    _pickView.showsSelectionIndicator = YES;
    _viewTop.layer.masksToBounds = YES;
    _viewTop.layer.borderWidth = 1;
    _viewTop.layer.borderColor = [kd6d6d6 CGColor];
    if (_type .integerValue == 3) {
        [_cleanButton setTitle:@"清除" forState:UIControlStateNormal];
        if (_dataArray.count > 0) {
            NSString *nameAPassword = _dataArray[0];
            NSArray *namePasswordArray = [nameAPassword componentsSeparatedByString:@"|-+"];
            _shopName = namePasswordArray[0];
            _shopId = namePasswordArray[1];
        }
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _dataArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (_type.integerValue == 1) {
        ShopModel *model = _dataArray[row];
        return model.name;
    }else if (_type.integerValue == 3){
        NSString *nameAPassword = _dataArray[row];
        NSArray *namePasswordArray = [nameAPassword componentsSeparatedByString:@"|-+"];
        NSString *name = namePasswordArray[0];
        return name;
    }
    else{
        NSString *name = _dataArray[row];
        return name;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (_type.integerValue == 1) {
        ShopModel *model = _dataArray[row];
        //    NSLog(@"%@ - %@",model.name,model.id);
        _shopName = model.name;
        _shopId = model.id;
    }else if (_type.integerValue == 3){
        NSString *nameAPassword = _dataArray[row];
        NSArray *namePasswordArray = [nameAPassword componentsSeparatedByString:@"|-+"];
        _shopName = namePasswordArray[0];
        _shopId = namePasswordArray[1];
    } else{
        _shopName = _dataArray[row];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
